<?php

include_once ("TaskHubDB.class.php");

class Account {
	/*
        public uint GetAccountIDByEmail(string Email)
        public uint GetAccountIDByTelephoneNumber(string TelephoneNumber)
		public AccountInformation GetAccountInformation(uint AccountID)
        public uint CreateAccount(string Email, string TelephoneNumber, string Name, string Password)
        public bool ChangeAccountInformation(uint AccountID, string Email, string TelephoneNumber, string Name, string Password)
        public uint AuthenticateAccount(string Email, string Password)
	*/

	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Static session handling methods

	public static function StartSession () {
		//session_start ();
	}

	public static function RequireLoggedIn () {
		//if (! $_SESSION["logged_in"]) {
		//	header ("HTTP/1.0 403 Forbidden");
		//	exit (0);
		//}
	}

	public static function SetLoggedIn ($logged_in) {
		$_SESSION["logged_in"] = $logged_in;
	}

	public static function GetAccountID ($account_id) {
		return $_SESSION["account_id"];
	}
	public static function SetAccountID ($account_id) {
		$_SESSION["account_id"] = $account_id;
	}


	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Public WS methods

	public function GetAccountIDByEmail ($data) {
		Account::RequireLoggedIn ();
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetAccountIDByEmail ($params["Email"]));
	}

	public function GetAccountIDByTelephoneNumber ($data) {
		Account::RequireLoggedIn ();
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetAccountIDByTelephoneNumber ($params["TelephoneNumber"]));
	}

	public function GetAccountInformation ($data) {
		//Account::RequireLoggedIn ();
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_GetAccountInformation ($params["AccountID"]));
	}

	public function CreateAccount ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_CreateAccount (
			$params["Email"], $params["TelephoneNumber"], $params["Name"], $params["Password"]
		));
	}

	public function ChangeAccountInformation ($data) {
		Account::RequireLoggedIn ();
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_ChangeAccountInformation (
			$params["Email"], $params["TelephoneNumber"], $params["Name"], $params["Password"]
		));
	}

	public function AuthenticateAccount ($data) {
		$params = get_object_vars($data);
		return Array (__FUNCTION__."Result" => $this->_AuthenticateAccount (
			$params["Email"], $params["Password"]
		));
	}


	//////////////////////////////////////////////////////////////////////////////////////////////////
	//  Private raw methods

	private function _GetAccountIDByEmail ($email) {
		$result = mysql_query ("SELECT AccountID FROM Accounts WHERE Email = '{$email}'");
		if (! $result) return null;
		$row = mysql_fetch_assoc ($result);
		return $row["AccountID"];
	}

	private function _GetAccountIDByTelephoneNumber ($telephone_number) {
		$result = mysql_query ("SELECT AccountID FROM Accounts WHERE TelephoneNumber = '{$telephone_number}'");
		if (! $result) return null;
		$row = mysql_fetch_assoc ($result);
		return $row["AccountID"];
	}

	private function _GetAccountInformation ($AccountID) {
		$result = mysql_query ("SELECT (Email,TelephoneNumber,Name) FROM Accounts WHERE AccountID = '{$AccountID}'");
		if (! $result)
			return null;
		$row = mysql_fetch_assoc ($result);
		return $row;
	}

	private function _CreateAccount ($email, $telephone_number, $name, $password) {
		// Check if account already exists //
		$account_id = $this->_GetAccountIDByEmail ($email);
		if ($account_id != null)
			return null;

		$account_id = $this->_GetAccountIDByTelephoneNumber ($telephone_number);
		if ($account_id != null)
			return null;

		// Insert new Account //
		$result = mysql_query ("INSERT INTO Accounts (Email,TelephoneNumber,Name,Password) VALUES ('{$email}', '{$telephone_number}', '{$name}', '{$password}')");
		if (! $result) {
			TaskHubDB::AddError (mysql_error ());
			return null;
		}

		// Get new account ID //
		$account_id = mysql_insert_id ();
		Account::SetLoggedIn (true);
		Account::SetAccountID ($account_id);
		return $account_id;
	}

	private function _ChangeAccountInformation ($email, $telephone_number, $name, $password) {
		$account_id = Account::GetAccountID ();

		// Modify the Account //
		$query = "UPDATE Accounts SET Email = '{$Email}', TelephoneNumber = '{$TelephoneNumber}', Name = '{$Name}', Password = '{$Password}' " .
				 "WHERE AccountID = '{$account_id}'";
		$result = mysql_query ($query);
		if (! $result)
			return false;

		return true;
	}

	private function _AuthenticateAccount ($email, $password) {
		// Find the account ID //
		$account_id = $this->_GetAccountIDByEmail ($email);
		if ($account_id == null)
			return null;

		// Check if the password matches //
		$result = mysql_query ("SELECT Password FROM Accounts WHERE AccountID = '{$account_id}'");
		$row = mysql_fetch_assoc ($result);

		if ($row["Password"] == $password) {
			Account::SetLoggedIn (true);
			Account::SetAccountID ($account_id);
			return $account_id;
		}
	}

}

?>